﻿using System;

using FineCollection.Intf;

using NUnit.Framework;

namespace FineCollection.Cont
{


    [TestFixture]
    public class HashTable_test
    {
        
        [Test]
        public void test_Count ()
        {
            HashTable<long> ht = new HashTable<long>();            
            Assert.AreEqual(0, ht.Count);

            ht.Add(1234567890, 333);
            Assert.AreEqual(1, ht.Count);

            ht.Add(9876543210, 444);
            Assert.AreEqual(2, ht.Count);

            ht.Add(10, 555);
            Assert.AreEqual(3, ht.Count);
        }


        [Test]
        public void test_Contains_InEmptyTable()
        {
            HashTable<long> ht = new HashTable<long>();
            bool exists = ht.Contains(1L, 1);
            Assert.IsFalse(exists);
        }


        [Test]
        public void test_Contains_NotExists()
        {
            HashTable<long> ht = new HashTable<long> {{1L, 1}, {3L, 3}};
            bool exists = ht.Contains(2L, 2);
            Assert.IsFalse(exists);
        }


        [Test]
        public void test_Contains_Exists()
        {
            HashTable<long> ht = 
                new HashTable<long> { { 1L, 1 }, {2L, 2}, { 3L, 3 } };
            bool exists = ht.Contains(2L, 2);
            Assert.IsTrue(exists);
        }


        [Test]
        public void test_Iterator_1()
        {
            HashTable<long> ht = 
                new HashTable<long> { { 1L, 1 }, { 2L, 2 }, { 3L, 3 }, { 4L, 4 } };

            long[] arr = IterateAllAndSort(ht);

            Assert.AreEqual(1, arr[0]);
            Assert.AreEqual(2, arr[1]);
            Assert.AreEqual(3, arr[2]);
            Assert.AreEqual(4, arr[3]);
        }


        [Test]
        public void test_Iterator_Same()
        {
            HashTable<long> ht = 
                new HashTable<long> { { 10L, 1 }, { 20L, 2 }, { 20L, 2 }, { 20L, 2 }, { 30L, 3 } };

            long[] arr = IterateAllAndSort(ht);

            Assert.AreEqual(10, arr[0]);
            Assert.AreEqual(20, arr[1]);
            Assert.AreEqual(20, arr[2]);
            Assert.AreEqual(20, arr[3]);
            Assert.AreEqual(30, arr[4]);
        }


        private static TT[] IterateAllAndSort<TT>(RCollection<TT> col)
            where TT: IComparable<TT>
        {
            int n = col.Count;
            TT[] arr = new TT[n];
            using (RIterator<TT> it = col.TakeIterator())
                for (int i = 0; i < n && it.Next(); i++)
                    arr[i] = it.Current;
            Array.Sort(arr);
            return arr;
        }
        

    }


}